package com.company.practice;



import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * @description: some desc
 * @author: ruoan
 * @date: 2021/1/5 16:37
 */
public class WeakHashMapTest {


    public static void main(String[] args) {


        fun1();

        //fun2();


    }

    private static void fun2() {
        //WeakHashMap<String, Object> maps = new WeakHashMap<>();
        //String a = "a";
        //Object o = new Object();

        WeakHashMap<String,Object> maps = new WeakHashMap<>();
        //Integer k = new Integer(1);
        String k = new String("1");
        Object a = new Object();
        String k2 = new String("11");
        Object a2 = new Object();


        maps.put(k,a);
        //maps.put(k2,a2);
        System.out.println("收集前："+ maps);

        //a = "b";
        //System.out.println("收集前："+ maps);


        k = null;
        System.out.println("收集前："+ maps);
        System.gc();


        System.out.println("收集后："+ maps);
    }

    private static void fun1() {
        WeakHashMap<String, Map<String, Object>> maps = new WeakHashMap<>();
        String a = new String("a");
        Object o = new Object();
        Map<String, Object> val = new HashMap<>();
        val.put(new String("b"), o); //这里key如果不用b而用a的话，则达不到效果
        maps.put(a, val);

        //WeakHashMap<String,Object> maps = new WeakHashMap<>();
        ////String a = "a";
        //String a = new String("a");
        //Object o = new Object();
        //maps.put(a, o);




        System.out.println("收集前："+maps);

        a = null;
        System.gc();


        System.out.println("收集后："+ maps);
    }
}
